package com.jh.paymentchannel.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.jh.paymentchannel.util.Util;
import com.jh.paymentchannel.util.ump.exception.VerifyException;
import com.jh.paymentchannel.util.ump.paygate.v40.HttpRequest;
import com.jh.paymentchannel.util.ump.paygate.v40.Mer2Plat_v40;
import com.jh.paymentchannel.util.ump.paygate.v40.Plat2Mer_v40;
import com.jnewsdk.util.SignUtil;

import cn.jh.common.utils.Md5Util;
import net.sf.json.JSONObject;


@Controller
@EnableAutoConfiguration
public class SFPageBackService {

	private static final Logger log = LoggerFactory.getLogger(SFPageBackService.class);
	@Autowired
	Util util;
	
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/sf/pageback")
	public @ResponseBody Object topupRequest(HttpServletRequest request,
			@RequestParam(value = "mch_id") String mch_id,
			@RequestParam(value = "password") String password,
			@RequestParam(value = "secretKey") String secretKey,
			 @RequestParam(value = "out_trade_no") String ordercode
			 ) throws IOException{
		log.info("into pageback  ================ ");
		Map<String, Object> map = new TreeMap<String, Object>(
		         new Comparator<String>() {
		             @Override
					public int compare(String obj1, String obj2) {
		             return obj1.compareTo(obj2);
		           }
		        });
				map.put("sp_id", "1102-2");  
				map.put("mch_id", mch_id);  
				map.put("out_trade_no", ordercode);
				map.put("password", password);
				map.put("nonce_str", ordercode);
			//拼接字符串
			String signMsg = SignUtil.getSignMsg(map, null);
			log.info("拼接字符串——signMsg@"+signMsg);
		    String key = secretKey;
		    String signature = Md5Util.getMD5(signMsg+"&key="+key).toUpperCase(); 
		    System.out.println("signature:"+signature);
		    map.put("sign", signature);  
		    log.info("---------------------——map@"+map.toString());
		    signMsg = SignUtil.getSignMsg(map, null);
			String url = "http://api.shangfudata.com/gate/epay/submit";
			log.info("--------------------——signMsg@"+signMsg);
			String msg = HttpRequest.sendPost(url, signMsg);
			log.info("---------------------——msg@"+msg.toString());
			if(!msg.contains("{")) {
				msg = "{"+msg+"}";
			}
			Map mapp = new HashMap();
			if(msg.isEmpty()||null==msg) {
				mapp.put("message", "响应数据为空");
				mapp.put("code", "failed");	
				mapp.put("channel_type", "sf_pay");
				return map;
			}
			JSONObject json = JSONObject.fromObject(msg); 
			log.info("--------------------——json@"+json);
			if("SUCCESS".equals(json.getString("status"))) {
				if("SUCCESS".equals(json.getString("result_code"))) {
					mapp.put("code", "success");
				}else if("PROCESSING".equals(json.getString("result_code"))){
					mapp.put("code", "PROCESSING");
				}
				else {
					mapp.put("message", json.getString("err_msg"));
					mapp.put("code", "failed");	
					mapp.put("channel_type", "sf_pay");
				}
			}else {
				mapp.put("message", json.getString("message"));
				mapp.put("code", "failed");	
				mapp.put("channel_type", "sf_pay");
			}
	       return mapp;
	}
		
	
}
